﻿
function checkFileType(obj) {
    var file = obj.files[0];
    if (file == undefined) {
        $("#imageErr").html("Please upload your image.");
        return false;
    }
    var pos = file.fileName.lastIndexOf(".");
    var fileExt = file.fileName.substring(pos, file.fileName.length).toLowerCase();
    if (fileExt != ".jpg" && fileExt != ".png" && fileExt != ".gif") {
        $("#imageErr").html("File type must be jpg,gif or png");
        return false;
    }
    $("#imageErr").html("");//重置错误信息
    return true;
}

function checkWatermarkText(obj){
    if (obj.value == "") {
        $("#waterMarkTextErr").html("Please input your watermark text.");
        return false;
    }
    $("#waterMarkTextErr").html("");//重置错误信息
    return true;
}

function validateInput() {
    if (!checkFileType($("[name=image]")[0]) | !checkWatermarkText($("[name=watermark]")[0])) {
        $("[type=submit]")[0].click=false;
    }
}

$(document).ready(function () {
    $(":submit").click(function (check) {
        if (!checkFileType($("[name=image]")[0]) | !checkWatermarkText($("[name=watermark]")[0])) {
            check.preventDefault(); //此处阻止提交表单  
        }
    });
}); 
